//
//  XiuGaiMiMaViewController.m
//  ZhouProject
//
//  Created by Dlrj on 14-12-18.
//  Copyright (c) 2014年 admin. All rights reserved.
//

#import "XiuGaiMiMaViewController.h"
#import "LoadingHelper.h"
#import "NetWorkingHelper.h"

@implementation XiuGaiMiMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"修改密码";
    
    self.navigationItem.hidesBackButton = YES;

    UILabel * labPassword = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(150), kW(80), kH(25))];
    labPassword.text = @"原密码:";
    labPassword.textColor = [UIColor blackColor];
    [self.view addSubview:labPassword];
    UILabel * labPasswordconfirm = [[UILabel alloc]initWithFrame:CGRectMake(kW(20), kH(200), kW(80), kH(25))];
    labPasswordconfirm.text = @"新密码:";
    labPasswordconfirm.textColor = [UIColor blackColor];
    [self.view addSubview:labPasswordconfirm];
    
    //原密码
    _txtPassword = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(145), kW(200), kH(33.5))];
    _txtPassword.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPassword.returnKeyType = UIReturnKeyDone;
    _txtPassword.delegate = self;
    _txtPassword.placeholder = @"原密码";
    UILabel *leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPassword setLeftView:leftView];
    [_txtPassword setLeftViewMode:UITextFieldViewModeAlways];
    _txtPassword.secureTextEntry = YES;
    _txtPassword.clearButtonMode = UITextFieldViewModeAlways;
    _txtPassword.layer.cornerRadius = 5.0;
    //边框宽度
    _txtPassword.layer.borderWidth = 1.0f;
    //边框颜色
    _txtPassword.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txtPassword.clipsToBounds = YES;
    [self.view addSubview:_txtPassword];
    
    //新密码
    _txtPasswordconfirm = [[UITextField alloc]initWithFrame:CGRectMake(kW(100), kH(195), kW(200), kH(33.5))];
    _txtPasswordconfirm.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    _txtPasswordconfirm.returnKeyType = UIReturnKeyDone;
    _txtPasswordconfirm.delegate = self;
    _txtPasswordconfirm.placeholder = @"新密码";
    leftView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kW(5), kH(33.5))];
    leftView.backgroundColor = [UIColor colorWithRed:241.0/255 green:240.0/255 blue:245.0/255 alpha:1];
    [_txtPasswordconfirm setLeftView:leftView];
    [_txtPasswordconfirm setLeftViewMode:UITextFieldViewModeAlways];
    _txtPasswordconfirm.secureTextEntry = YES;
    _txtPasswordconfirm.clearButtonMode = UITextFieldViewModeAlways;
    _txtPasswordconfirm.layer.cornerRadius = 5.0;
    //边框宽度
    _txtPasswordconfirm.layer.borderWidth = 1.0f;
    //边框颜色
    _txtPasswordconfirm.layer.borderColor = [UIColor colorWithRed:212.0 / 255 green:212.0 / 255 blue:212.0 / 255 alpha:1].CGColor;
    //内容不超出边框
    _txtPasswordconfirm.clipsToBounds = YES;
    [self.view addSubview:_txtPasswordconfirm];
    
    //确定按钮
    UIButton * btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(kW(40), kH(300), kW(100), kH(35));
    [btnLogin setBackgroundImage:[UIImage imageNamed:@"hb4"] forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLogin setTitle:@"确定" forState:UIControlStateNormal];
    //设置圆角
    btnLogin.layer.cornerRadius = 5.0;
    //内容不超出边框
    btnLogin.clipsToBounds = YES;
    [btnLogin addTarget:self  action:@selector(clickqueding) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    //取消按钮
    UIButton * btnReg = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnReg.frame = CGRectMake(kW(180), kH(300), kW(100), kH(35));
    [btnReg setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnReg setBackgroundImage:[UIImage imageNamed:@"hb1"] forState:UIControlStateNormal];
    [btnReg setTitle:@"取消" forState:UIControlStateNormal];
    //设置圆角
    btnReg.layer.cornerRadius = 5.0;
    //内容不超出边框
    btnReg.clipsToBounds = YES;
    [btnReg addTarget:self  action:@selector(clickquxiao) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnReg];

}

//确定按钮点击方法，调用修改密码接口
-(void)clickqueding
{
    
    if(_txtPassword.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"修改失败" message:@"请填写原密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    if(_txtPasswordconfirm.text.length == 0)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"修改失败" message:@"请填写新密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    if(_txtPasswordconfirm.text.length < 6)
    {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"修改失败" message:@"新密码至少6位" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }
    
    [LoadingHelper showLoadingWithView:self.view];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
       NSString * str =  [NetWorkingHelper XiuGaiMiMaWithuserId:self.struserId  passwd:_txtPassword.text resetPasswd:_txtPasswordconfirm.text];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [LoadingHelper hiddonLoadingWithView:self.view];
            //修改密码成功
            if ([str isEqualToString:@"1"]) {
                //保存密码
                [self defaultSavePassword:_txtPasswordconfirm.text];
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改密码成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                alert.tag = 100;
                [alert show];
            }
            //修改密码失败
            else {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改密码失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
        });
    });
}

//保存密码
-(void)defaultSavePassword:(NSString*)a {
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:a  forKey:@"Password"];
    [defaults synchronize];
}

//提示框代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //修改密码成功－确定
    if (alertView.tag == 100) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//取消按钮点击方法
-(void)clickquxiao {
    [self.navigationController popViewControllerAnimated:YES];
}

//捕获点击事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [_txtPassword resignFirstResponder];
    [_txtPasswordconfirm resignFirstResponder];
}

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _txtPassword || textField == _txtPasswordconfirm){           //密码：length_15
        if (range.location >= 15) {
            return NO;
        }
        return YES;
    }
    else {
        return YES;
    }
}

//done按键点击方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [_txtPassword resignFirstResponder];
    [_txtPasswordconfirm resignFirstResponder];

    return YES;
}

//输入框代理方法
-(BOOL)textFieldShouldClear:(UITextField *)textField {
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
